﻿// <copyright file="DebugModeCollection.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>10/16/2009</date>
// <summary>A collection of debug modes.</summary>

// License:

// Product: Engine.csproj
// Author: Robert N. Tenney
// Copyright: 10/16/2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace TenneySoftware.Engine.Debug {
    /// <summary>
    /// A collection of debug modes.
    /// </summary>
    public class DebugModeCollection : Collection<DebugMode> {
        /// <summary>
        /// Term to search for.
        /// </summary>
        private string searchTerm;

        /// <summary>
        /// Initializes a new instance of the DebugModeCollection class.
        /// </summary>
        public DebugModeCollection() : base(new List<DebugMode>()) {
        }

        /// <summary>
        /// Sets all of the values in the list to the given value.
        /// </summary>
        /// <param name="val">Value to set list to.</param>
        public void SetAll(bool val) {
            List<DebugMode> modes = this.Items as List<DebugMode>;
            for (int iter = 0; iter < modes.Count; iter++) {
                DebugMode mode = modes[iter];
                mode.Value = val;
            }
        }

        /// <summary>
        /// Finds a DebugMode item in the list using the specified predicate.
        /// </summary>
        /// <param name="match">Predicate to use for the search.</param>
        /// <returns>The DebugMode found in the list.</returns>
        public DebugMode Find(Predicate<DebugMode> match) {
            List<DebugMode> modes = this.Items as List<DebugMode>;
            return modes.Find(match);
        }

        /// <summary>
        /// Finds a DebugMode item in the list with the given name.
        /// </summary>
        /// <param name="name">Name of the DebugMode to search for.</param>
        /// <returns>The DebugMode found in the list.</returns>
        public DebugMode FindByName(string name) {
            this.searchTerm = name;
            return this.Find(this.Search);
        }

        /// <summary>
        /// Sets the value of a DebugMode object based on the name of the mode.
        /// </summary>
        /// <param name="name">Name of the DebugMode.</param>
        /// <param name="val">Value to set the mode to.</param>
        public void SetValue(string name, bool val) {
            DebugMode mode = this.FindByName(name);
            mode.Value = val;
        }

        /// <summary>
        /// Checks to see if a DebugMode with a given name is in the list.
        /// </summary>
        /// <param name="name">Name to search for.</param>
        /// <returns>True if there is a DebugMode by the given name in the list.</returns>
        public bool ContainsName(string name) {
            DebugMode mode = this.FindByName(name);
            if (mode != null) {
                return true;
            } else {
                return false;
            }
        }

        /// <summary>
        /// Checks to see if a given DebugMode is set or not.
        /// </summary>
        /// <param name="name">Name of the DebugMode.</param>
        /// <returns>True if set, false if not.</returns>
        public bool GetValue(string name) {
            return this.FindByName(name).Value;
        }

        /// <summary>
        /// Predicate to search the list looking for matches to name.
        /// </summary>
        /// <param name="mode">DebugMode to compare.</param>
        /// <returns>True if the names are the same.</returns>
        private bool Search(DebugMode mode) {
            return string.Compare(mode.Name, this.searchTerm, StringComparison.OrdinalIgnoreCase) == 0;
        }
    }
}
